package com.vcredit.creditcat.manager.product;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.ICreditProductManager;
import com.vcredit.creditcat.dto.product.MerchantProductDto;
import com.vcredit.creditcat.dto.product.ProductExpireItemDto;
import com.vcredit.creditcat.dto.speedloan.SpeedloanAuthItemDto;
import com.vcredit.creditcat.enumtype.ProductAuditResultEnum;
import com.vcredit.creditcat.manager.CustProductManager;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.service.CustService;
import com.vcredit.creditcat.service.auth.AuthItemExtService;
import com.vcredit.creditcat.service.product.CreditProductService;
import com.vcredit.creditcat.service.product.util.EnumExtUtil;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.BeanUtil;
import com.vcredit.framework.util.VelocityUtil;

/**
 *
 * @author 张新功
 * @version 2017年5月25日 下午4:47:31
 */
@Service(protocol = {"protocol1"}, version = "1.0", group = "product")
@Qualifier(value = "product")
public class CreditProductManager implements ICreditProductManager {

  @Autowired
  private CreditProductService creditProductService;
  @Autowired
  private CustProductManager custProductManager;
  @Autowired
  private CustProductApplyService custProductApplyService;
  @Autowired
  private CustService custService;
  @Autowired
  private AuthItemExtService authItemExtService;

  /**
   * 产品申请额度提交
   * @throws Exception
   */
  @Override
  public ProductExpireItemDto applyCommit(Long custId, Long productId,
      Map<String, String> otherParams) throws Exception {
    return creditProductService.applyCommit(custId, productId, otherParams);
  }

  @Log
  @Override
  public String getStatus(Long custId, Long productId, Long type, MerchantProductDto dto) throws Exception {
    Cust cust = custService.findById(custId);
    CustProductApply custProductApply = new CustProductApply();
    BeanUtil.copy(dto, custProductApply);
    if(custProductApply.getAuditResult() == null) {
    	custProductApply = null;
    }
    boolean canApplyAgin = custProductApplyService.canProductApplyAgin(custProductApply);
    
    if(!canApplyAgin) { // 不能再次申请
    	String currentStatusDesc = null;
	    ProductAuditResultEnum auditResultEnum = EnumExtUtil
	        .getEnumOnValue(ProductAuditResultEnum.class, String.valueOf(custProductApply.getAuditResult()), "code");
	    if(null == auditResultEnum) {
	    	return VelocityUtil.templateToText("template/productStatus02.vm");
	    }
	    switch (auditResultEnum) {
	      case ApplyFailed:
	        if (null == type) {
	          currentStatusDesc = VelocityUtil.templateToText("template/productStatus05.vm");
	        }
	        
	        else {
	            if (custProductApply.getIsApply()) {
	              String creditApplyTime = custProductApply.getCreditApplyTime();
	              if (StringUtils.hasLength(creditApplyTime) && creditApplyTime.length() > 10) {
	                creditApplyTime = creditApplyTime.substring(5, 10).replace("-",".");
	              }
	              currentStatusDesc = VelocityUtil
	                  .templateToText("template/productStatus08.vm", "expireTime", creditApplyTime);
	            } else {
	              currentStatusDesc = VelocityUtil.templateToText("template/productStatus05.vm");
	            }
	        }
	        break;
	      case ApplySuccess:
	        if (null == type) {
	          currentStatusDesc = VelocityUtil.templateToText("template/productStatus07.vm", "amount",
	        		  custProductApply.getCreditAmt());
	        } else { 
	          currentStatusDesc = VelocityUtil.templateToText("template/productStatus04.vm");
	        }
	        break;
	      case Appling:
	        currentStatusDesc = VelocityUtil.templateToText("template/productStatus03.vm");
	        break;
	      case Never:
	        break;
	      case UnAuth:
	        break;
	      case MaskAppling:
	          currentStatusDesc = VelocityUtil.templateToText("template/productStatus03.vm");
	          break;
	      case ApplyFailedCreditAgain:
	          currentStatusDesc = VelocityUtil.templateToText("template/productStatus06.vm");
	          break;
	      case SpeedloanCommitting:
	          currentStatusDesc = VelocityUtil.templateToText("template/productStatus03.vm");
	          break;
	      case LoanApplying:
	          currentStatusDesc = auditResultEnum.getDesc();
	          break;
	      case LoanApplyNoPass:
	          currentStatusDesc = auditResultEnum.getDesc();
	          break;
	      case LoanApplyPass:
	          currentStatusDesc = auditResultEnum.getDesc();
	          break;
	      case Reoayment:
	          currentStatusDesc = auditResultEnum.getDesc();
	          break;
	      case Expired:
	          currentStatusDesc = auditResultEnum.getDesc();
	          break;
	      case End:
	          currentStatusDesc = auditResultEnum.getDesc();
	          break;
	      default:
	        currentStatusDesc = VelocityUtil.templateToText("template/productStatus02.vm");
	        break;
	    }
		return currentStatusDesc;
    }
    
      // 查询用户没有做或者认证失败的认证项
//      List<SpeedloanAuthItemDto> speedloanAuthItems = authItemExtService.getUnDoOrFailedSpeedloanAuthItem(cust, productId);
//      if(!Utility.isNullOrEmpty(speedloanAuthItems)) {
//    	  return VelocityUtil.templateToText("template/productStatus01.vm", "num", speedloanAuthItems.size());
//      }

    return VelocityUtil.templateToText("template/productStatus02.vm");
  }

  @Override
  public CustProductApply getAppliedItem(Long custId, Long productId) {
    CustProductApply newApplyProduct = custProductApplyService
        .findNewApplyProduct(productId, custId);
    return newApplyProduct;
  }

}
